package com.edu.zut.security;

import com.edu.zut.entity.User;
import com.edu.zut.entity.UserRole;
import com.edu.zut.mapper.RoleMapper;
import com.edu.zut.mapper.UserMapper;
import com.edu.zut.mapper.UserRoleMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author 许文科
 * @Date 2023/1/15 13:43
 * @Description paperdetect-bcak
 */
@SpringBootTest
public class register {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Test
    public void show1() {

        //插入用户
        User insertUser = new User();
        insertUser.setUserName("zhangsan");
        //ROLE_student
        insertUser.setUserType("2");
        //密码加密
        String encodePassword = passwordEncoder.encode("123456");
        insertUser.setPassword(encodePassword);
        int insertFlag = userMapper.insert(insertUser);
        System.out.println("插入用户标记：" + insertFlag);


        Long userId = userMapper.getUserIdByUserName("zhangsan");
        Long roleId = roleMapper.getRoleIdByRoleName("ROLE_student");
        //将role_id和user_id插入到sys_user_role表中
        UserRole user_Role = new UserRole();
        user_Role.setUserId(userId);
        user_Role.setRoleId(roleId);
        int insertUserRoleFlag = userRoleMapper.insert(user_Role);

        System.out.println("插入标记insertUserRoleFlag：" + insertUserRoleFlag);

    }
}
